package multidraw;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import multidraw.model.DrawingCanvasModel;
import multidraw.model.ModelObserver;
import multidraw.model.UserStatusObserver;

/**
 * 
 * @author Hayley Abbas
 * @version 1.0
 * Modified by Michael Anderson.
 */
public class ClientListPanelView extends JPanel implements UserStatusObserver {

	private DrawingCanvasModel myModel;
	
	private JLabel myName;
	private JList clientList;
	private DefaultListModel listModel;
	
	private JPanel buttonPanel;
	private JButton transferButton;
	private JButton revokeButton;
	
	public ClientListPanelView(DrawingCanvasModel m) {
		myModel = m;
		
		// TODO: a problem with this method is a problem of redundant unrelated updates
		// messages. Because when an images draw also invoke the update method
		// of this class which is bad.
		myModel.attachUserStatusObserver(this);
		//setSize(200, 300);
		//setPreferredSize(new Dimension(100, 600));
		//setVisible(true);
		
		setLayout(new BorderLayout());
		
		//myName = new JLabel("<html>Logged in as <i>" + name + "</i></html>");
		myName = new JLabel("Please log in");
		add(myName, BorderLayout.NORTH);
		
		listModel = new DefaultListModel();
		clientList = new JList(listModel);
		add(clientList, BorderLayout.CENTER);
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(2,1,5,5));
		add(buttonPanel, BorderLayout.SOUTH);
		
		transferButton = new JButton("Transfer Control");
		buttonPanel.add(transferButton);
		
		revokeButton = new JButton("Revoke Control");
		buttonPanel.add(revokeButton);
		
		addClientListPanelListener(new ClientListPanelController(myModel, this));
		//updateContent();
		updateUserStatus();
	}
	
	/* Note
	 * This eventually needs to be a client 
	 */
	
	protected void addClientListPanelListener(ClientListPanelController controller) {
		transferButton.addActionListener((ActionListener)controller);
		revokeButton.addActionListener((ActionListener)controller);
	}
	
	
	
	public void updateUserList(Vector<String> userList) {
		if (userList == null) {
			listModel.clear();
			return;
		}
		listModel.clear();
		for (int i = 0; i < userList.size(); i++) {
			listModel.add(i, userList.get(i));
		}
	}
	
	public JButton getTransferButton() {
		return transferButton;
	}
	
	public JButton getRevokeButton(){
		return revokeButton;
	}
	
	public String getSelectedUser(){
	    String name = (String) clientList.getSelectedValue();
	    String[] temp = name.split(" ");
	    return temp[0];
	}

	@Override
	public void updateUserStatus() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		//repaint();
		//update the user list				
		if (!myModel.getLoggedInStatus()) {
			//clean this panel
			myName.setText("<html>Offline mode</html>");
			updateUserList(null);
			transferButton.setEnabled(false);
			revokeButton.setEnabled(false);
			repaint();
			return;
		} else {
			myName.setText("<html>Logged in as <i>" + myModel.getName() + "</i></html>");
			// if sessioin ID null
			if (myModel.getSessionID() == null) {
				//clean the client list.
				transferButton.setEnabled(false);
				
			} else {
				transferButton.setEnabled(true);
			}
			updateUserList(myModel.getClientList());
			repaint();
			
		}
		
		if (myModel.getSessionID() == null) {
			transferButton.setEnabled(false);
			revokeButton.setEnabled(false);
			return;
		}
		
		
		if (myModel.getName().equals(myModel.getController())) {
			transferButton.setEnabled(true);
			revokeButton.setEnabled(false);
		}
		else if (myModel.getName().equals(myModel.getOwner())) {
			transferButton.setEnabled(false);
			revokeButton.setEnabled(true);
		}
		else {
			transferButton.setEnabled(false);
			revokeButton.setEnabled(false);
		}
		
	}
	

}
